<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:countryDict:add']"
        >新增</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="序号" type="index" align="center" min-width="5%"/>
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="洲名称(中文)" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link :to="'/system/country-data/index/' + scope.row.id" class="link-type">
            <span>{{ scope.row.cnName }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="洲名称(英文)" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ scope.row.enName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="洲名称(日文)" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ scope.row.jaName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:countryDict:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:countryDict:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="洲名称(中文):" prop="cnName">
          <el-input v-model="form.cnName" placeholder="请输入洲名称(中文)" />
        </el-form-item>
        <el-form-item label="洲名称(英文):" prop="enName">
          <el-input v-model="form.enName" placeholder="请输入洲名称(英文)" />
        </el-form-item>
        <el-form-item label="洲名称(日文):" prop="jaName">
          <el-input v-model="form.jaName" placeholder="请输入洲名称(日文)" />
        </el-form-item>
        <el-form-item label="显示排序" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { pageList,getInfo,add,edit,remove } from "@/api/triones/countryDict";
export default {
  name: "index",
  data() {
    return {
      loading: true,
      showSearch: true,
      total: 0,
      dataList: [],
      title: "",
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        level: 1,
        parentId: 0
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cnName: [
          { required: true, message: "洲名称（中文）不能为空", trigger: "blur" }
        ],
        enName: [
          { required: true, message: "洲名称（英文）不能为空", trigger: "blur" }
        ],
        jaName: [
          { required: true, message: "洲名称（日文）不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      pageList(this.queryParams).then(response => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id
      getInfo(id).then(response => {
        this.form = response.data;
        this.title = "修改洲信息";
        this.open = true;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加洲信息";
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        level: 1,
        parentId: 0,
        cnName: undefined,
        enName: undefined,
        jaName: undefined,
        nationalityCountryCode: undefined,
        sort: 0
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            this.$modal.confirm('是否确认该操作？').then(() => {
              edit(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            })
          } else {
            this.$modal.confirm('是否确认该操作？').then(() => {
              add(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            })
          }
        }
      });
    },
    /** 取消 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除编号为"' + id + '"的数据项？').then(function() {
        return remove(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
}
</script>
<style scoped lang="scss">

</style>
